package com.module.sysconfig.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.module.sysconfig.entity.Ywlxpz;
import com.module.sysconfig.entity.Zdb;
import com.module.sysconfig.service.ZdbService;
import com.support.helper.AjaxHelper;
import com.support.util.StringUtils;
import com.support.web.ControllerSupport;
/**
 * 字典表
 * @author 吕乐帅
 *
 */
@Controller
@Scope(value = "prototype")
@RequestMapping("/sysconfig/zdb")
public class ZdbController extends ControllerSupport{
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ZdbService zdbService;
	
	/**
	 * 新增字典数据
	 * @throws IOException
	 */
	@RequestMapping("/saveZdb")
	 public void saveZdb() throws IOException
	 {
		 try{
				
			 
			 Map<String, String>  paramsMap =  this.getParamsMap();
			 	Zdb zdb=new Zdb();
				String ZDMC = paramsMap.get("ZDMC");
				String DM = paramsMap.get("DM");
				String ZHI = paramsMap.get("ZHI");
				String QT = paramsMap.get("QT");
				String CJR = paramsMap.get("CJR");
				String CJRMC = paramsMap.get("CJRMC");
				String ZHXGR = paramsMap.get("ZHXGR");
				String ZHXGRMC = paramsMap.get("ZHXGRMC");
				String BZ1 = paramsMap.get("BZ1");
				String BZ2 = paramsMap.get("BZ2");
				String BZ3 = paramsMap.get("BZ3");
				String BZ4 = paramsMap.get("BZ4");
				
				
				zdb.setZdmc(ZDMC ==null ? "": ZDMC);
				zdb.setDm(DM ==null ? "": DM);
				zdb.setZhi(ZHI ==null ? "": ZHI);
				zdb.setQt(QT ==null ? "": QT);
				zdb.setCjr(CJR ==null ? "": CJR);
				zdb.setCjrmc(CJRMC ==null ? "": CJRMC);
				zdb.setZhxgr(ZHXGR ==null ? "": ZHXGR);
				zdb.setZhxgrmc(ZHXGRMC ==null ? "": ZHXGRMC);
				zdb.setSfhf(1);
				zdb.setBz1(BZ1 == null ? "":BZ1);
				zdb.setBz2(BZ2 == null ? "":BZ2);
				zdb.setBz3(BZ3 == null ? "":BZ3);
				zdb.setBz4(BZ4 == null ? "":BZ4);
				/*if () {
					
				}*/
				zdbService.saveZdb(zdb);
		 
		 returnJSONView(AjaxHelper.toJSON(zdb, AjaxHelper.SUCCESS, "新增字典数据成功"));
		 }catch (Exception e) {
				e.printStackTrace();
				logger.error("ZdbController.saveZdb:", e);
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, e.getMessage()));
			}
		 }
	
	
	/**
	 * 修改字典数据
	 * @throws IOException
	 */
	 @RequestMapping("/updateZdb")
	 public void updateZdb() throws IOException
	 {
		 try{
			 Map<String, String>  paramsMap =  this.getParamsMap();
			 String id = paramsMap.get("ID");
				if(StringUtils.isEmpty(id))
				{
					returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, "缺少主键ID"));
					return;
				}
				
			 Zdb zdb=zdbService.queryZdbById(Long.parseLong(id));
			 	String ZDMC = paramsMap.get("ZDMC");
				String DM = paramsMap.get("DM");
				String ZHI = paramsMap.get("ZHI");
				String QT = paramsMap.get("QT");
				String CJR = paramsMap.get("CJR");
				String CJRMC = paramsMap.get("CJRMC");
				String ZHXGR = paramsMap.get("ZHXGR");
				String ZHXGRMC = paramsMap.get("ZHXGRMC");
				String BZ1 = paramsMap.get("BZ1");
				String BZ2 = paramsMap.get("BZ2");
				String BZ3 = paramsMap.get("BZ3");
				String BZ4 = paramsMap.get("BZ4");
				
				
				zdb.setZdmc(ZDMC ==null ? zdb.getZdmc(): ZDMC);
				zdb.setDm(DM ==null ? zdb.getDm(): DM);
				zdb.setZhi(ZHI ==null ? zdb.getZhi(): ZHI);
				zdb.setQt(QT ==null ? zdb.getQt(): QT);
				zdb.setCjr(CJR ==null ? zdb.getCjr(): CJR);
				zdb.setCjrmc(CJRMC ==null ? zdb.getCjrmc(): CJRMC);
				zdb.setZhxgr(ZHXGR ==null ? zdb.getZhxgr(): ZHXGR);
				zdb.setZhxgrmc(ZHXGRMC ==null ? zdb.getZhxgrmc(): ZHXGRMC);
				zdb.setSfhf(1);
				zdb.setBz1(BZ1 == null ? zdb.getBz1():BZ1);
				zdb.setBz2(BZ2 == null ? zdb.getBz2():BZ2);
				zdb.setBz3(BZ3 == null ? zdb.getBz3():BZ3);
				zdb.setBz4(BZ4 == null ? zdb.getBz4():BZ4);
		 
			 zdbService.updateZdb(zdb);
		 returnJSONView(AjaxHelper.toJSON(zdb, AjaxHelper.SUCCESS, "更新字典数据成功"));
	 }catch (Exception e) {
			e.printStackTrace();
			logger.error("ZdbController.saveZdb:", e);
			returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, e.getMessage()));
		}
	 }
	 
	 
	 /**
	  * 删除字典数据
	  * @throws IOException
	  */
	 @RequestMapping("/deleteZdb")
		public void deleteZdb() throws IOException
		{
			try{
				Map<String, String> paramsMap = this.getParamsMap();
				String id = paramsMap.get("ID");
				if(null == id)
				{
					returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, "缺少主键ID"));
					return;
				}
				zdbService.deleteZdb(Long.parseLong(id));
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.SUCCESS, "删除字典表数据！"));
				
			}catch (Exception e) {
				e.printStackTrace();
				logger.error("ZdbService.savezdb:", e);
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, e.getMessage()));
			}
		}
	 
	 /**
	  * 查询字典数据
	  * @throws IOException
	  */
	 @RequestMapping("/queryZdb")
		public void queryZdb() throws IOException
		{
			try{
				
				 List<Map<String, Object>> bdckList = zdbService.queryZdb();
				returnJSONView(AjaxHelper.toJSON(bdckList, AjaxHelper.SUCCESS, "字典数据查询！"));
				
			}catch (Exception e) {
				e.printStackTrace();
				logger.error("ZdbController.saveZdb:", e);
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, e.getMessage()));
			}
		}
	 
	 
	 /**
	  * 按ID查询字典数据
	  * @throws IOException
	  */
	 @RequestMapping("/queryZdbById")
		public void queryZdbById() throws IOException
		{
			try{
				Map<String, String> paramsMap = this.getParamsMap();
				
				String ID = paramsMap.get("ID");
				if(StringUtils.isNumeric(ID)){
				returnJSONView(AjaxHelper.toJSON(zdbService.queryZdbById(Long.parseLong(ID)), AjaxHelper.SUCCESS, "字典数据查询！"));
				}
			}catch (Exception e) {
				e.printStackTrace();
				logger.error("ZdbController.saveZdb:", e);
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, e.getMessage()));
			}
		}
	 /**
	  * 按ID查询字典数据
	  * @throws IOException
	  */
	 @RequestMapping("/queryZdbByZdmc")
		public void queryZdbByZdmc() throws IOException
		{
			try{
				Map<String, String> paramsMap = this.getParamsMap();
				
				String ZDMC=paramsMap.get("ZDMC");
				if(StringUtils.areNotEmpty(ZDMC)){
				returnJSONView(AjaxHelper.toJSON(zdbService.queryZdbByZdmc(ZDMC), AjaxHelper.SUCCESS, "字典数据查询成功！"));
				}
			}catch (Exception e) {
				e.printStackTrace();
				logger.error("ZdbController.saveZdb:", e);
				returnJSONView(AjaxHelper.toJSON(null, AjaxHelper.FAILURE, e.getMessage()));
			}
		}
	 
	 
	 
	 
}
